package com.auxgroup.smarthome.job;

import com.auxgroup.smarthome.entity.UserDeviceDemoEntity;
import com.auxgroup.smarthome.repo.UserDeviceDemoEntityRepository;
import com.auxgroup.smarthome.utils.common.LOG;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by kevinchen on 2017/8/14.
 * 简单类型
 * SimpleJob
 */
@Component
public class UserDeviceSimpleJob implements SimpleJob {

    @Resource
    private UserDeviceDemoEntityRepository repository;

    @Override
    public void execute(ShardingContext shardingContext) {
        LOG.info(this,"任务id:"+shardingContext.getTaskId()+" 任务名："+shardingContext.getJobName() + "  job参数："+ shardingContext.getJobParameter()+ " 分片名："+ shardingContext.getShardingParameter()+ " 分片值：" + shardingContext.getShardingItem()+ " 总分片数量："+shardingContext.getShardingTotalCount()+" =====SimpleJob====== ");
        List<UserDeviceDemoEntity> list = repository.getPartDataList(shardingContext.getShardingItem(),UserDeviceDemoEntity.class);
        list.stream().forEach(userDeviceDemoEntity -> {
            if (userDeviceDemoEntity.getTime() > System.currentTimeMillis()){
                LOG.dubug( this,"用户id:"+userDeviceDemoEntity.getUid()+"***********did:"+userDeviceDemoEntity.getDid() +"，发送控制指令："+ userDeviceDemoEntity.getEvents());
            }
        });
    }
}
